1154G - Minimum Possible LCM - CodeForces Solution


brute force greedy math number theory *2200

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long
#define pb push_back
#define fi first
#define se second
#define el '\n'
#define ss ' '
#define ll long long
#define vii vector<pair<int,int>>
#define ii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define rall(a) a.rbegin(),a.rend()
#define all(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#define popcount(x) __builtin_popcount(x)
#define zedanPasha ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define clr(x,y) memset(x,y,sizeof(x))

template<typename T=int>
istream& operator >>(istream& in,vector<T>& a)
{
    for(T&i:a) cin>>i;
    return in;
}
template<typename T=int>
ostream& operator <<(ostream& out,vector<T>& a)
{
    for(T&i:a) cout<<i<<ss;
    return out;
}
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

int dx[] = {0, 0, -1, 1};//right,left,up,down
int dy[] = {1, -1, 0, 0};

bool valid(int r, int c, int R, int C)
{
    if (r < 0 || r >= R || c < 0 || c >= C)
        return false;
    return true;
}

ll fp(ll a, ll b)
{
    ll ans=1;
    while (b)
    {
        if(b&1)
            ans*=a;
        a*=a;
        b>>=1;
    }
    return ans;
}
int n;
vi a;
int const N = 1e7 + 5;
int mp[N];

int f,s;
void sieve()
{
    ll mn = LLONG_MAX;
    for(int i=1 ; i<=N ; i++)
    {
        ll sm1=0,sm2=0;
        for(int j=i ; j<N ; j+=i)
        {
            if(mp[j])
            {
                int cnt = mp[j];
                if(!sm1)
                {
                    sm1 = j;
                    if(cnt-1)
                    {
                        sm2 = j;
                        break;
                    }
                }
                else
                {
                    sm2 = j;
                    break;
                }
            }
        }
        ll cur = (sm1*sm2)/i;
        if(sm1 && sm2 && mn>cur)
        {
            f = sm1,s = sm2;
            mn = cur;
        }
    }
}
void doWork()
{
    cin>>n;
    a.resize(n);
    for(int i=0 ; i<n ; i++)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    sieve();
    vll ans;
    for(int i=0 ; i<n ; i++)
    {
        if(a[i]==f)
        {
            ans.pb(i+1);
            f=-1;
        }
        else if(a[i]==s)
        {
            ans.pb(i+1);
            s=-1;
        }
        if(sz(ans)==2)
            break;
    }
    sort(all(ans));
    cout<<ans;
}

signed main()
{
    zedanPasha
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1;
    //cin >> t;
    while (t--)
        doWork();
    return 0;
}


Comments

Submit
0 Comments
More Questions

152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set